home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Atari Compendium
/
The Atari Compendium (Toad Computers) (1994).iso
/
files
/
umich
/
network
/
ka9q
/
nhclb120.zoo
/
mac_io.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-02-11
|
20KB
|
1,144 lines
/* OS- and machine-dependent stuff for Mac */
/*mac_io.c
* Routines for Macintosh IO and file stuff
*/
#include <stdio.h>
#include "global.h"
#include "mbuf.h"
#include "internet.h"
#include "iface.h"
#include "mac.h"
#include "cmdparse.h"
#include "DeviceMgr.h"
#include "WindowMgr.h"
#include "EventMgr.h"
#include "SerialDvr.h"
#include "HFS.h"
#include <time.h>
extern long timezone;
extern errno;
struct Store_input Store_input[ASY_MAX];
struct asy asy[ASY_MAX];
static ioParam MacSer;
struct interface *ifaces;
static struct RemoveIt {
struct RemoveIt *next;
char *name_ptr;
} *Head;
/* Called at startup time to set up console I/O, memory heap */
ioinit()
{
timezone = 60*60+1;
tzname[0] = "CST";
tzname[1] = "CST";
Click_On(0);
Head = malloc(sizeof (struct RemoveIt));
Head->next = NULL;
Head->name_ptr = NULL;
return(0);
}
/* Called just before exiting to restore console state */
iostop()
{
int i;
struct RemoveIt *rptr;
while(ifaces != NULLIF){
if(ifaces->stop != NULLFP)
(*ifaces->stop)(ifaces);
ifaces = ifaces->next;
}
/*
* I want to close down all the files and then remove the (possibly still existing) files
* because the MAC will not allow the file to be removed when it still has an open file
* descriptor
*/
for( i = 3; i < _NFILE; i++)
{
close(i);
}
(void)unlink("dir.temp");
rptr = Head;
while( rptr->next != NULL)
{
if ( rptr->name_ptr != NULL)
unlink(rptr->name_ptr);
rptr = rptr->next;
}
}
/* Initialize asynch port "dev" */
int slipisopen = 0;
int iref;
int oref;
char Recv_buf[MAX_STORE];
char Send_buf[MAX_STORE];
int
asy_init(dev,arg1,arg2,bufsize)
int16 dev;
char *arg1,*arg2;
unsigned bufsize;
{
register struct asy *ap;
register struct interface *if_asy;
extern struct interface *ifaces;
char ser_name[255];
struct Store_input *store;
OsErr e;
ap = &asy[dev];
ap->tty = malloc(strlen(arg2)+1);
strcpy(ap->tty, arg2);
#ifdef DEBUG
printf("asy_init: as->tty = %s, dev = %d\n", ap->tty, dev);
#endif
if ( ap == NULL)
{
return(-1);
}
switch ( ap->tty[0] )
{
case 'a':
case 'A':
if ( ap->devopen == 0 )
{
e = RAMSDOpen(sPortA);
if ( e != noErr )
{
printf("RAMSDOpen failed, e = %d\n",e);
SysBeep(4);
ExitToShell();
}
e = OpenDriver ("\p.AIn", &iref);
if ( e != noErr) {
printf("OpenDriver for AIn failed, e = %d\n",e);
SysBeep (4);
ExitToShell ();
}
e = OpenDriver ("\p.AOut", &oref);
if ( e != noErr) {
printf("OpenDriver for AOut failed, e = %d\n",e);
SysBeep (4);
CloseDriver (iref);
ExitToShell ();
}
ap->portIn = AinRefNum;
ap->portOut = AoutRefNum;
ap->devopen = 1;
}
else
{
printf("Device %c is already open.\n", ap->tty[0]);
return(-1);
}
break;
case 'b':
case 'B':
if ( ap->devopen == 0)
{
e = RAMSDOpen(sPortB);
if ( e != noErr )
{
printf("RAMSDOpen failed, e = %d\n", e);
SysBeep(4);
ExitToShell();
}
e = OpenDriver ("\p.BIn", &iref);
if ( e != noErr) {
printf("OpenDriver for BIn failed, e = %d\n", e);
SysBeep (4);
ExitToShell ();
}
e = OpenDriver ("\p.BOut", &oref);
if ( e != noErr) {
printf("Opendriver for Bout failed e = %d\n",e);
SysBeep (4);
CloseDriver (iref);
ExitToShell ();
}
ap->portIn = BinRefNum;
ap->portOut = BoutRefNum;
ap->devopen = 1;
}
else
{
printf("Device %c is already open.\n", ap->tty[0] );
return(-1);
}
break;
default:
printf("Unknown device %c, could not configure port,\n", ap->tty);
return(-1);
}
store = &Store_input[dev];
store->head = store->store;
store->tail = store->store;
store->amt = 0;
e = SerSetBuf( ap->portIn, Recv_buf, MAX_STORE);
if ( e != noErr) {
printf("asy_init: SerSetBuf error on %d\n", ap->portIn);
SysBeep (4);
CloseDriver (iref);
ExitToShell ();
}
SerSetBuf( ap->portOut, Send_buf, MAX_STORE);
if ( e != noErr) {
printf("asy_init: SerSetBuf error on %d\n", ap->portOut);
SysBeep (4);
CloseDriver (iref);
ExitToShell ();
}
slipisopen = 1;
return (0);
}
int
asy_stop(interface)
struct interface *interface;
{
register struct asy *ap;
int ref;
ap = &asy[interface->dev];
if (slipisopen)
{
MacSer.ioActCount = 0;
MacSer.ioRefNum = ap->portIn;
MacSer.ioCompletion = 0;
MacSer.ioBuffer = ap->recv_buf;
MacSer.ioReqCount = 0;
MacSer.ioPosMode = 1;
(void) PBKillIO(&MacSer, FALSE);
MacSer.ioActCount = 0;
MacSer.ioRefNum = ap->portOut;
MacSer.ioCompletion = 0;
MacSer.ioBuffer = ap->recv_buf;
MacSer.ioReqCount = 0;
MacSer.ioPosMode = 1;
(void) PBKillIO(&MacSer, FALSE);
FSClose(ap->portIn);
FSClose(ap->portOut);
if ( ap->portIn = AinRefNum)
{
OpenDriver("\p.Ain", &ref);
OpenDriver("\p.Aout", &ref);
}
else
{
OpenDriver("\p.Bin", &ref);
OpenDriver("\p.Bout", &ref);
}
slipisopen = 0;
}
}
/* Asynchronous line I/O control */
asy_ioctl(interface,argc,argv)
struct interface *interface;
int argc;
char *argv[];
{
if(argc < 1){
printf("%d\r\n",asy[interface->dev].speed);
return 0;
}
return asy_speed(interface->dev,atoi(argv[0]));
}
/* Set asynch line speed */
int
asy_speed(dev,speed)
int dev;
int speed;
{
OsErr e;
int serialconfig;
SerShk HandShake;
struct asy *ap;
ap = &asy[dev];
if(speed == 0 || dev >= nasy)
return(-1);
#ifdef DEBUG
printf("asy_speed: Setting speed for device %d to %d\n",dev, speed);
#endif
asy[dev].speed = speed;
switch(speed)
{
case 300:
serialconfig = baud300;
break;
case 600:
serialconfig = baud600;
break;
case 1200:
serialconfig = baud1200;
break;
case 1800:
serialconfig = baud1800;
break;
case 2400:
serialconfig = baud2400;
break;
case 3600:
serialconfig = baud3600;
break;
case 4800:
serialconfig = baud4800;
break;
case 7200:
serialconfig = baud7200;
break;
case 9600:
serialconfig = baud9600;
break;
case 19200:
serialconfig = baud19200;
break;
case (long)57600:
serialconfig = baud57600;
break;
default:
printf("asy_speed: Unknown speed (%ld)\n", speed);
break;
}
/*
printf("serialconfig = %d\n", serialconfig);
*/
serialconfig |= (stop10|noParity|data8);
e = SerReset( ap->portIn, serialconfig);
if ( e != noErr) {
printf("asy_speed: could not set config for %d\n", ap->portIn);
SysBeep (4);
CloseDriver (iref);
ExitToShell ();
}
bzero(&HandShake, sizeof (SerShk));
e = SerHShake(ap->portIn, &HandShake);
if ( e != noErr) {
printf("asy_speed: could not set handshake for %d\n", ap->portIn);
SysBeep (4);
CloseDriver (iref);
ExitToShell ();
}
e = SerReset( ap->portOut, serialconfig);
if ( e != noErr) {
printf("asy_speed: could not set config for %d\n", ap->portOut);
SysBeep (4);
ExitToShell ();
}
bzero(&HandShake, sizeof (SerShk));
e = SerHShake(ap->portOut, &HandShake);
if ( e != noErr) {
printf("asy_speed: could not set handshake for %d\n", ap->portOut);
SysBeep (4);
CloseDriver (iref);
ExitToShell ();
}
/*
printf("asy_speed: completed.\n");
*/
return(0);
}
/* Send a buffer to serial transmitter */
asy_output(dev,buf,cnt)
unsigned dev;
char *buf;
unsigned short cnt;
{
register struct asy *ap;
long amount = (long)cnt;
ap = &asy[dev];
/*
printf("asy_output called. dev = %x, cnt = %d\n", dev, cnt);
*/
if(dev >= nasy)
return(-1);
FSWrite(ap->portOut,&amount, buf);
return(0);
}
/*
* Receive characters from asynch line
* Returns count of characters read
*/
unsigned
asy_recv(dev,buf,cnt)
int dev;
char *buf;
unsigned cnt;
{
long amount[8];
int tot = 0;
struct Store_input *store;
char store_it[MAX_STORE];
int got = 0;
int tt;
struct asy *ap;
#ifdef DEBUG
printf("asy_recv: dev = %d\n", dev);
#endif
if ( dev > nasy )
{
printf("Error on asy_recv. dev = %d, max = %d\n", dev, nasy);
return(-1);
}
ap = &asy[dev];
store = &Store_input[dev]; /* point to current */
/* printf("asy_recv: want %d, buffer has %d bytes\n", cnt, store->amt); */
if ( cnt > 0 && store->amt > 0)
{
tt = min(cnt , store->amt);
tt = min(tt, MA